package LinkedList;

public class ListNode {

    int val;
    ListNode next;

    public ListNode() {
    }

    ListNode(int x) {
        val = x;
        next = null;
    }
    ListNode(int x, ListNode y){
        val = x;
        next = y;
    }

    public static boolean hasRing(ListNode head) {
        if (head == null)
            return false;
        ListNode slow = head;
        ListNode fast = head;
        while (slow != null && fast != null) {
            slow = slow.next;
            if (fast.next == null)
                break;
            fast = fast.next.next;
            if (slow == fast)
                return true;
        }
        return false;
    }

    public static void displayList(ListNode head) {
        if (head == null) {
            System.out.println("Empty ListNode");
            return;
        }

        if (hasRing(head)) {
            System.out.println("Ring ListNode can't display");
            return;
        }

        ListNode cur = head;
        while (cur != null) {
            System.out.printf(" %d ", cur.val);
            if (cur.next != null)
                System.out.printf("->");
            cur=cur.next;
        }
        System.out.printf("\n");
    }

    public static ListNode generateList(int[] nums) {
        ListNode dummyHead = new ListNode();
        ListNode cur = dummyHead;
        for (int num:nums) {
            cur.next = new ListNode(num);
            cur=cur.next;
        }
        cur.next=null;

        return dummyHead.next;
    }


}
